package action;

import java.awt.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dao.AnswerDao;
import dao.impl.AnswerDaoImpl;
import dao.impl.QuestionDaoImpl;
import domain.Answer;

public class InputAnswerAction extends Action {
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		 Answer answer=(Answer)form; 
		 AnswerDao answerDao=new AnswerDaoImpl();
		
		 int questionId=32;
		
			 if(request.getParameter("questionId")!=null)
				 questionId=Integer.parseInt(request.getParameter("questionId"));
			 else if(request.getAttribute("questionId")!=null)
				 questionId= (Integer)request.getAttribute("questionId");
			 else mapping.findForward("listAnswer");
			 System.out.println("questionId:-----"+questionId );
		 if(questionId>0){
				 answer.setQuestion(new QuestionDaoImpl().findById(questionId));
				 answerDao.save(answer);
					 }
		 
		 System.out.println(answer.getContent());
		 String path = request.getContextPath();
		 	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		 	ActionForward actionForward = new ActionForward(basePath+"forum/listAnswer.jsp?questionId="+questionId,true);
				return actionForward;
		   
	}

}
